package org.as3search.criteria {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import flash.errors.IllegalOperationError;
	
	import org.flexunit.Assert;
	
	import org.as3search.Search;
	import org.as3search.config.FlexSearchConfiguration;
	
	/**
	 * 
	 */
	public class SimpleExpressionTestCase {
		
		private var classToTestRef:SimpleExpression;
		
		public function SimpleExpressionTestCase() {
			
		}
		
		[Before]
		public function setup():void {
			this.classToTestRef = new SimpleExpression("title", "abc", Search.configuration, false);
		}
		
		[Test]
		public function testEvaluate():void {
			try {
				classToTestRef.evaluate({noTitle: null});
				Assert.fail("evaluate() did not throw an error on an undefined " +
					"property.");
			} catch (e1:ArgumentError) {
//				pass!
			}
		}
		
		[Test]
		public function testEvaluateProperty():void {
			try {
				classToTestRef.evaluate({title: ""});
				Assert.fail("evaluateProperty() did not throw an IllegalOperationError");
			} catch (e1:IllegalOperationError) {
//				pass!
			}
		}
		
		[Test]
		public function testSimpleExpression():void {
			this.classToTestRef = new SimpleExpression("1", "2", Search.configuration, false);
			Assert.assertEquals("constructor did not set 'propertyName' correctly",
				classToTestRef.propertyName, "1");
			Assert.assertEquals("constructor did not set 'value' correctly",
				classToTestRef.value, "2");
			Assert.assertEquals("constructor did not set 'ignoreCase' correctly",
				classToTestRef.ignoreCase, false);
			
			this.classToTestRef = new SimpleExpression("1", "2", null, true);
			Assert.assertTrue("constructor did not set 'ignoreCase' correctly",
				classToTestRef.ignoreCase);
			Assert.assertEquals("constructor did not set 'configuration' correctly",
				Search.configuration, classToTestRef.configuration);
		}
		
		[Test]
		public function testConfiguration():void {
			var newConf:FlexSearchConfiguration = new FlexSearchConfiguration();
			classToTestRef.configuration = newConf;
			Assert.assertEquals("failed to set 'configuration'",
				newConf, classToTestRef.configuration);
			classToTestRef.configuration = null;
			Assert.assertEquals("failed to default over to Search.configuration",
				Search.configuration, classToTestRef.configuration);
		}
		
		[Test]
		public function testPropertyResolver():void {
			Assert.assertEquals("failed to return the config's property resolver",
				Search.configuration.propertyResolver, classToTestRef.propertyResolver);
			var conf:FlexSearchConfiguration = new FlexSearchConfiguration();
			classToTestRef.configuration = conf;
			Assert.assertEquals("failed to return the new config's property resolver",
				conf.propertyResolver, classToTestRef.propertyResolver);
		}
	}
}